/**
 * Copyright &copy; 2013-2016 山东易科德软件有限公司 All rights reserved.
 */
package cn.net.ecode.modules.cms.web;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import cn.net.ecode.common.config.Global;
import cn.net.ecode.common.persistence.Page;
import cn.net.ecode.common.utils.DateUtils;
import cn.net.ecode.common.utils.StringUtils;
import cn.net.ecode.common.web.BaseController;
import cn.net.ecode.modules.cms.entity.Site;
import cn.net.ecode.modules.cms.service.FileTplService;
import cn.net.ecode.modules.cms.service.SiteService;
import cn.net.ecode.modules.cms.utils.CmsUtils;
import cn.net.ecode.modules.sys.utils.UserUtils;

/**
 * 站点Controller
 * @author ThinkGem
 * @version 2014-9-16
 */
@Controller
@RequestMapping(value = "${adminPath}/cms/site")
public class SiteController extends BaseController {

	@Autowired
	private SiteService siteService;
    @Autowired
   	private FileTplService fileTplService;
	
	@ModelAttribute
	public Site get(String siteCode, boolean isNewRecord) {
		CmsUtils.getSite(Site.MAIN_SITE_CODE); // 用于初始化站点数据
		return siteService.get(siteCode, isNewRecord);
	}
	
//	@RequiresPermissions("cms:site:view")
	@RequestMapping(value = {"list", ""})
	public String list(Site site, Model model) {
		return "modules/cms/siteList";
	}
	
//	@RequiresPermissions("cms:site:view")
	@ResponseBody
	@RequestMapping(value = "listData")
	public Page<Site> listData(Site site, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<Site> page = siteService.findPage(new Page<Site>(request, response), site); 
		return page;
	}

//	@RequiresPermissions("cms:site:view")
	@RequestMapping(value = "form")
	public String form(Site site, Model model) {
        model.addAttribute("indexViewList", fileTplService.getTplContent(Site.DEFAULT_TEMPLATE));
        model.addAttribute("site_DEFAULT_TEMPLATE", Site.DEFAULT_TEMPLATE);
		model.addAttribute("site", site);
		return "modules/cms/siteForm";
	}

	@RequiresPermissions("cms:site:edit")
	@ResponseBody
	@RequestMapping(value = "save")
	public String save(Site site) {
		StringBuilder message = new StringBuilder();
		if (!beanValidator(message, site)){
			return renderResult(Global.FALSE, message);
		}
		siteService.save(site);
		return renderResult(Global.TRUE, "保存站点成功！");
	}
	
	@RequiresPermissions("cms:site:edit")
	@ResponseBody
	@RequestMapping(value = "delete")
	public String delete(Site site, Boolean isRe, RedirectAttributes redirectAttributes) {
		if (Site.isMainSite(site.getSiteCode())){
			return renderResult(Global.FALSE, "删除站点失败, 不允许删除默认站点");
		}else{
			siteService.delete(site, isRe);
			return renderResult(Global.FALSE, (isRe != null && isRe ? "恢复" : "") + "删除站点成功");
		}
	}
	
	/**
	 * 选择站点
	 */
	@RequestMapping(value = "select")
	public String select(String siteCode, String redirect, HttpServletResponse response){
		if (StringUtils.isBlank(siteCode)){
			return renderResult(Global.FALSE, "切换站点失败！没有设置站点编码！");
		}
		UserUtils.putCache("currentSiteCode", siteCode);
//		// 保存到Cookie中，下次登录后自动切换到该站点
//		String cookieName = "siteCode";
//		if (Global.isUseCorpModel()){
//			cookieName = CorpUtils.getCurrentCorpCode() + "_" + cookieName;
//		}
//		CookieUtils.setCookie(response, cookieName, siteCode);
		if (StringUtils.isNotBlank(redirect)){
			return "redirect:" + redirect;
		}
		return renderResult(response, Global.TRUE, "切换站点成功！");
	}
	
	/** 
	 * 重建站点文章索引
	 */
	@RequiresPermissions("cms:site:editIndex")
	@ResponseBody
	@RequestMapping(value = "editIndex")
	public String editIndex(Site site, RedirectAttributes redirectAttributes) throws IllegalArgumentException, IllegalAccessException, InstantiationException, IOException, InvocationTargetException, NoSuchMethodException  {
		long s = System.currentTimeMillis();
		siteService.rebulidIndex(site, true, true);
		return renderResult(Global.TRUE, "站点重建索引成功！ 用时"+DateUtils.formatDateTime(System.currentTimeMillis()-s));
	}
}
